/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.com.develop.store.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mx.com.develop.store.model.Cliente;

/**
 *
 * @author Equipo
 */
//@WebServlet(name = "registroCliente", urlPatterns = {"/registro_cliente.do"})
public class RegistroCliente extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            int n = (int) (Math.random() * 3 + 1);
            String img = "imagenes/" + n + ".png";
            request.setAttribute("img", img);
            request.getRequestDispatcher("registro_cliente.jsp").forward(request, response);
        } finally {
            out.close();
        }
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("se ejecuta init(config)");
        super.init(config); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void init() throws ServletException {
        System.out.println("Se ejecuta init() solito");
        super.init(); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("Se ejecuta el metodo service que castea");
        super.service(req, res); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("se ejecuta services de http");
        super.service(req, resp); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void destroy() {
        System.out.println("########################## adios ###################");
        super.destroy(); //To change body of generated methods, choose Tools | Templates.
    }
    
    
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        ServletContext cntxt = request.getServletContext();
        Enumeration<String> captchas = cntxt.getInitParameterNames();
        boolean vali = false;
        while(captchas.hasMoreElements()){
            String c=cntxt.getInitParameter(captchas.nextElement());
            String captchaform =(String)request.getParameter("captcha");
            if(c.equals(captchaform)){
                vali = true;
                break;
            }
        }
        if(vali){
            Cliente c = new Cliente();
            c.setNombre(request.getParameter("nombre"));
            c.setEdad(Integer.parseInt(request.getParameter("edad")));
            c.setContrasenia(request.getParameter("pass"));
            c.setDireccion(request.getParameter("direccion"));
            c.setUsuario(request.getParameter("usuario"));
            c.setTelefono(request.getParameter("telefono"));

            request.setAttribute("cliente", c);
            
            List<Cliente> clientes = (List<Cliente>) cntxt.getAttribute("clientes");
            if(clientes == null){
                clientes = new ArrayList<>();
                clientes.add(c);
            }else{
                clientes.add(c);
            }
            cntxt.setAttribute("clientes", clientes);
            request.getRequestDispatcher("registro_cliente_success.jsp").forward(request, response);
        }else{
            request.getRequestDispatcher("login_error.jsp").forward(request, response);
        }
    }
}
